package com.migu.rbac.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO : 菜单表
 * @Author: zj
 * @Date: 14:17 2019/3/14
 * @Version 1.0
 **/
@Data
public class SysMenu {

    private Integer id;

    private String menuname;

    private String parentid;

    private String href;

    private String icon;

    private String liclass;

    private String aclass;

    private String atarget;

    private String sclass;

    private String menutype;

    private String apilist;

    private String projectid;

    private Integer position;

    private List<SysMenu> menuList = new ArrayList<>();

    @JsonIgnore
    private List<SysRole> roles;

    /**
     * 孩子节点排序
     */
    public void sortChildren() {
        this.menuList.sort((m1, m2) -> {
            int orderBy1 = m1.getPosition() == null ? 1 : m1.getPosition();
            int orderBy2 = m2.getPosition() == null ? 1 : m2.getPosition();
            return orderBy1 < orderBy2 ? -1 : (orderBy1 == orderBy2 ? 0 : 1);
        });
        // 对每个节点的下一层节点进行排序
        for (SysMenu n : menuList) {
            if (n != null && n.getMenuList() != null) {
                n.sortChildren();
            }
        }
    }

    public SysMenu(Integer id, String menuname, String parentid, String href, String icon, String liclass, String aclass, String atarget, String sclass, String menutype, String apilist) {
        this.id = id;
        this.menuname = menuname;
        this.parentid = parentid;
        this.href = href;
        this.icon = icon;
        this.liclass = liclass;
        this.aclass = aclass;
        this.atarget = atarget;
        this.sclass = sclass;
        this.menutype = menutype;
        this.apilist = apilist;
    }

    public SysMenu() {
    }
}
